<?php

namespace app\home\admin;
use app\admin\controller\Admin;
use app\home\model\User;
use function foo\func;
use think\Db;
use think\Loader;
use think\Request;

class Reports extends Admin
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index(Request $request)
    {
        $data = $request->param();
        $where['u.role'] = ['in',[1,2,3]];
        if (isset($data['role']) && $data['role'] != 4){
            $where['u.role'] = $data['role'];
        }
        if (isset($data['keyword']) && !empty($data['keyword'])){
            $where['u.nickname|u.phonenum'] = ['like',"%{$data['keyword']}%"];
        }
        if (!empty($data['create_time'])){
            $time = explode('~',$data['create_time']);
            $start = strtotime($time[0]);
            $end = strtotime($time[1]);
            $where['r.create_time'] = ['between',[$start,$end]];
        }
        isset($data['page']) ? $page = $data['page'] : $page = 1;
        isset($data['listrow']) ? $listrow = $data['listrow'] :  $listrow = 10;
        $res = db('home_reports')
            ->alias('r')
            ->join('home_users u','r.toid=u.id')
            ->join('home_users s','r.uid=s.id')
            ->where($where)
            ->field('r.id,s.nickname,r.pics2,r.contents,u.nickname toname,u.phonenum,r.create_time,u.role,r.toid')
            ->paginate(10)
            ->each(function ($item){
                if ($item['id'] != null){
                    $item['pics2'] = explode(',',$item['pics2']);
                    $item['count'] = \db('home_reports')->where('toid',$item['toid'])->count();
                    $item['create_time'] = date("Y-m-d H:i:s",$item['create_time']);
                    return $item;
                }else{
                    return null;
                }

        });
        if ($res->toarray()){
            $this->assign('reportlist',$res);
        }else{
            return $this->success('无数据');
        }

        return $this->fetch();


    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function add(Request $request)
    {
        //
        $data = $request->param();
        if ($request->isPost()){
            $insert['name'] = $data['name'];
            $insert['type'] = $data['type'];
            if ($insert['type'] != 1){
               $insert['price'] = $data['price1']."-".$data['price2'];
            }
            $insert['create_time'] = time();
            $res = \db('home_taps')->insert($insert);
            if ($res){
                return $this->success('新增成功');
            }else{
                return $this->error('添加失败');
            }
        }else{
            $type = $data['type'];
            $this->assign('type',$type);
            return $this->fetch();
        }
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request)
    {
        //
        $data = $request->param();
        if ($request->isPost()){
            $insert['id'] = $data['id'];
            $insert['name'] = $data['name'];
            $insert['type'] = $data['type'];
            if ($insert['type'] !== 1){
                $insert['price'] = $data['price1']."-".$data['price2'];
            }
            $res = \db('home_taps')->update($insert);
            if ($res){
                return $this->success('更新成功');
            }else{
                return $this->error('更新失败');
            }
        }
        $id = $data['id'];
        $info = \db('home_taps')->where('id',$id)->find();
        if ($info){
            if (!empty($info['price'])){
                $array = explode('-',$info['price']);
                $info['price1'] = $array[0];
                $info['price2'] = $array[1];
            }
            $this->assign('list',$info);
            $this->assign('type',$data['type']);
            return $this->fetch('add');
        }else{
            return $this->error('无数据');
        }
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function deltaps(Request $request)
    {
        $data = $request->param();
        if (isset($data['ids'])){
            $ids = $data['ids'];
            $where['id'] = ['in',$ids];
        }else{
            $where['id'] = $data['id'];
        }

        $res = \db('home_taps')->where($where)->delete();
        if ($res){
            return $this->success('删除成功');
        }else{
            return $this->error('删除失败');
        }
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function changestatus(Request $request)
    {
        $data = $request->param();
        if (!empty($data['id'])){
            $id = $data['id'];
        }else{
            return $this->error('参数缺失');
        }
        if ($data['val'] == 0){
            $update['status'] = 2;
        }elseif($data['val'] == 1){
            $update['status'] = 1;
        }
        $result = Db::table('rd_home_taps')->where('id',$id)->update($update);
        if ($result){
            return $this->success('操作成功');
        }else{
            return $this->error('操作失败');
        }
    }
}
